using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Brutal_Dare_Portable
{
    public class Truth : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;

        string firstLine = "", secondLine = "", thirdLine = "Did they tell the truth?", yes = "Yes", no = "No";

        bool questionLoaded = false;

        public Truth(Game game)
            : base(game)
        {
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.PaleGoldenrod);

            spriteBatch.Begin();

            spriteBatch.DrawString(((Game1)Game).logoFont, ((Game1)Game).logoText,
                new Vector2((int)((GraphicsDevice.Viewport.TitleSafeArea.Width / 2) - (((Game1)Game).logoFont.MeasureString(((Game1)Game).logoText).X / 2)), 5), Color.Black);

            spriteBatch.DrawString(((Game1)Game).subTextFont, firstLine,
                    new Vector2((int)((GraphicsDevice.Viewport.TitleSafeArea.Width / 2) - (((Game1)Game).subTextFont.MeasureString(firstLine).X / 2)),
                                (int)((GraphicsDevice.Viewport.TitleSafeArea.Height / 2) - 120)), Color.Black);
            spriteBatch.DrawString(((Game1)Game).subTextFont, secondLine,
                    new Vector2((int)((GraphicsDevice.Viewport.TitleSafeArea.Width / 2) - (((Game1)Game).subTextFont.MeasureString(secondLine).X / 2)),
                                (int)((GraphicsDevice.Viewport.TitleSafeArea.Height / 2) - 120 + ((Game1)Game).subTextFont.MeasureString(firstLine).Y)), Color.Black);
            spriteBatch.DrawString(((Game1)Game).subTextFont, thirdLine,
                    new Vector2((int)((GraphicsDevice.Viewport.TitleSafeArea.Width / 2) - (((Game1)Game).subTextFont.MeasureString(thirdLine).X / 2)),
                                (int)((GraphicsDevice.Viewport.TitleSafeArea.Height / 2) - 100 + ((Game1)Game).subTextFont.MeasureString(firstLine).Y +
                                            ((Game1)Game).subTextFont.MeasureString(secondLine).Y)), Color.Black);

            spriteBatch.DrawString(((Game1)Game).choiceFont, no,
                    new Vector2((int)(100), (int)((GraphicsDevice.Viewport.TitleSafeArea.Height / 2) - (((Game1)Game).choiceFont.MeasureString(no).Y / 2) + 90)), Color.CadetBlue);
            spriteBatch.DrawString(((Game1)Game).choiceFont, yes,
                    new Vector2((int)((GraphicsDevice.Viewport.TitleSafeArea.Width) - (((Game1)Game).choiceFont.MeasureString(yes).X) - 100),
                                (int)((GraphicsDevice.Viewport.TitleSafeArea.Height / 2) - (((Game1)Game).choiceFont.MeasureString(yes).Y / 2) + 90)), Color.Firebrick);


            
            spriteBatch.End();

            base.Draw(gameTime);
        }

        public override void Update(GameTime gameTime)
        {
            if (!questionLoaded)
            {
                LoadText();
            }

            base.Update(gameTime);
        }

        private void LoadText()
        {
            if (((Game1)Game).under18)
            {
                firstLine = ((Game1)Game).questions[2][((Game1)Game).rand.Next(0, ((Game1)Game).questions[2].Length)];
            }
            else
            {
                firstLine = ((Game1)Game).questions[3][((Game1)Game).rand.Next(0, ((Game1)Game).questions[3].Length)];
            }

            if (((Game1)Game).subTextFont.MeasureString(firstLine).X >= (GraphicsDevice.Viewport.TitleSafeArea.Width / 2))
            {
                string[] splitQuestion = firstLine.Split(' ');
                if (splitQuestion.Length % 2 == 0)
                {
                    firstLine = "";
                    for (int i = 0; i < (splitQuestion.Length / 2); i++)
                    {
                        firstLine += splitQuestion[i];
                        firstLine += " ";
                    }
                    secondLine = "";
                    for (int i = (splitQuestion.Length / 2); i<splitQuestion.Length; i++)
                    {
                        secondLine += splitQuestion[i];
                        secondLine += " ";
                    }
                }
                else
                {
                    firstLine = "";
                    for (int i = 0; i < (splitQuestion.Length / 2) + 1; i++)
                    {
                        firstLine += splitQuestion[i];
                        firstLine += " ";
                    }
                    secondLine = "";
                    for (int i = (splitQuestion.Length / 2) + 1; i < splitQuestion.Length; i++)
                    {
                        secondLine += splitQuestion[i];
                        secondLine += " ";
                    }
                }
            }
            questionLoaded = true;
        }
    }
}
